package br.com.ac.fatureweb.rest.service;

import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import br.com.ac.fatureweb.exception.AccessNaoAutorizadoException;
import br.com.ac.fatureweb.integration.model.OcorrenciaVO;
import br.com.ac.fatureweb.service.NgcLeituristaBean;
import br.com.ac.fatureweb.service.NgcOcorrenciaBean;
import br.com.ac.fatureweb.service.ValidarAcesso;

@Path("/ocorrenciaBean")
public class OcorrenciaJSONBean {

	@Inject
	private NgcLeituristaBean ngcLeituristaBean;

	@Inject
	private NgcOcorrenciaBean ngcOcorrenciaBean;

	@POST
	@Path("listarOcorrencia")
	@Produces(MediaType.APPLICATION_JSON)
	public Response listarOcorrencia(

	@FormParam("imei") final String imei,

	@FormParam("usuario") final Long usuario,

	@FormParam("senha") final String senha) {
		try {
			if (ValidarAcesso.execute(usuario, senha, imei, ngcLeituristaBean)) {

				List<OcorrenciaVO> lstOcorrencia = ngcOcorrenciaBean
						.listarOcorrencia();

				return Response.ok().entity(lstOcorrencia).build();
			}

			return Response.status(Status.NO_CONTENT).build();
		} catch (AccessNaoAutorizadoException anae) {
			return Response.status(Status.UNAUTHORIZED)
					.entity(anae.getMessage()).build();
		} catch (final Exception e) {
			return Response.status(Status.INTERNAL_SERVER_ERROR)
					.entity(e.getMessage()).build();
		}
	}

}
